package com.briup.jz.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.briup.jz.bean.BaseRole;
import com.briup.jz.bean.BaseRoleExample;
import com.briup.jz.bean.extend.BaseRoleExtend;
import com.briup.jz.dao.BaseRoleMapper;
import com.briup.jz.dao.extend.BaseRoleExtendMapper;
import com.briup.jz.service.IBaseRoleService;
import com.briup.jz.utils.CustomerException;
@Service
public class BaseRoleServiceImpl implements IBaseRoleService {
	@Autowired
	private BaseRoleMapper baseRoleMapper;
	@Autowired
	private BaseRoleExtendMapper baseRoleExtendMapper;
	@Override
	public void saveOrUpdate(BaseRole baseRole) throws CustomerException {
		if(baseRole.getId()!=null) {
			baseRoleMapper.updateByPrimaryKey(baseRole);
		} else {
			// 判断是否有同名的分类，如果有抛出异常
			BaseRoleExample example = new BaseRoleExample();
			example.createCriteria().andNameEqualTo(baseRole.getName());
			List<BaseRole> list = baseRoleMapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名的角色已存在");
			}
			baseRoleMapper.insert(baseRole);
		}

	}

	@Override
	public List<BaseRole> query(String name) {
		BaseRoleExample example = new BaseRoleExample();
		if(name!=null){
			example.createCriteria().andNameLike("%"+name+"%");
		}
		List<BaseRole> list = baseRoleMapper.selectByExample(example);
		return list;
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		BaseRole BaseRole = baseRoleMapper.selectByPrimaryKey(id);
		if(BaseRole == null) {
			throw new CustomerException("要删除的角色信息不存在");
		}
		baseRoleMapper.deleteByPrimaryKey(id);

	}

	@Override
	public List<BaseRoleExtend> cascadePrivilegeFindAll(long id) {
		return baseRoleExtendMapper.cascadePrivilegeFindAll(id);
	}

}
